package leetcode_201_300;

public class LeeCode_223 {
    public static void main(String[] args) {
        System.out.println(computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
        System.out.println(computeArea(0,0,0,0,-1,-1,1,1));
        System.out.println(computeArea(-2,-2,2,2,-1,-1,1,1));
        System.out.println(computeArea(-3,-3,3,-1,-2,-2,2,2));
    }
    private static int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int area1 = (ay2 - ay1) * (ax2 - ax1);
        int area2 = (by2 - by1) * (bx2 - bx1);
        int sum = area1 + area2;
        if (by2 <= ay1 || ay2 <= by1 || bx2 <= ax1 || ax2 <= bx1){
            return sum;
        }
        int ya = ay2 - ay1 + by2 - by1;
        int yj = Math.max(ay2, by2) - Math.min(ay1, by1);
        int xa = ax2 - ax1 + bx2 - bx1;
        int xj = Math.max(bx2, ax2) - Math.min(ax1, bx1);
        int same = (ya - yj) * (xa - xj);
        return sum - same;
    }
}
